var line1Option = {};
var line2Option = {};
var line3Option = {};
var line4Option = {};
var line5Option = {};
var aasessValue = "";
var parentCodes = "city.sd";
$(function() {
  var sdtypeId = "szsxbzzbsd"; //"03"; //指标分组
  var fsdtypeId = "szsxbzzbfsd";
  var ywbtypeId = "szsxbzzbywb";
  var wwbtypeId = "szsxbzzbwwb";
  var ywbwzztypeId = "szsxbzzbywbwzz";
  var aindex = "-1";
  var timelineLsngth = 0;
  var gridOption = {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  };
  var xAxisOption = {
    type: 'category',
    axisLabel: {
      interval: 0,
      rotate: 45,
      margin: 2,
      textStyle: {
        color: '#fff',
        fontSize: 20
      }
    }
  };
  var tooltipOption = {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  };
  var yAxisOption = {
    type: 'value',
    axisLabel: {
      show: true,
      textStyle: {
        color: '#036192'
      }
    },
    splitLine: { // 分隔线
      show: true, // 默认显示，属性show控制显示与否
      // onGap: null,
      lineStyle: { // 属性lineStyle（详见lineStyle）控制线条样式
        color: ['#052f54'],
        width: 1,
        type: 'solid'
      }
    },
  };
  var legendtextStyle = {
    color: "#92d050"
  };
  line1Option = {
    parentCode: "city.sd",
    tooltip: tooltipOption,
    legend: {
      textStyle: legendtextStyle
    },
    grid: gridOption,
    yAxis: yAxisOption,
    xAxis: {
      type: 'category',
      axisLabel: {
        show: true,
        textStyle: {
          color: '#fff',
          fontSize: 20
        }
      }
    }
  };
  line2Option = {
    parentCode: "city.fsd",
    tooltip: tooltipOption,
    legend: {
      textStyle: legendtextStyle
    },
    grid: gridOption,
    yAxis: yAxisOption,
    xAxis: xAxisOption
  };
  line3Option = {
    parentCode: "sz.ywb",
    tooltip: tooltipOption,
    legend: {
      right: '110px',
      y: 'top',
      textStyle: legendtextStyle
    },
    grid: gridOption,
    yAxis: yAxisOption,
    xAxis: xAxisOption
  };
  line4Option = {
    parentCode: "sz.wwb",
    tooltip: tooltipOption,
    legend: {
      right: '40px',
      y: 'top',
      textStyle: legendtextStyle
    },
    grid: gridOption,
    yAxis: yAxisOption,
    xAxis: xAxisOption
  };
  line5Option = {
    parentCode: "sz.ywbwzz",
    tooltip: tooltipOption,
    legend: {
      right: '40px',
      y: 'top',
      textStyle: legendtextStyle
    },
    grid: gridOption,
    yAxis: yAxisOption,
    xAxis: xAxisOption
  };

  function initAllData() {
    var line1 = echarts.init(document.getElementById("bar1"));
    $.ajax({
      url: contextPath + "/aasess/data/api/getDataByCode",
      data: {
        parentCode: "city.sd",
        aasessValueId: "",
        typeId: sdtypeId
      },
      type: "post",
      success: function(res) {
        var array = new Array();
        var dataset = res["dataset"];
        var timelines = res["aasessValue"];
        var series = new Array(dataset["dimensions"].length - 1);
        for (var index = 0; index < dataset["dimensions"].length - 1; index++) {
          series[index] = {
            type: 'bar',
            stack: '总量',
            label: {
              normal: {
                show: false,
                position: 'insideRight'
              }
            }
          };
        }
        for (var i = 0; i < dataset["source"].length; i++) {
          array.push(sum(dataset["source"][i]));
        }
        series[dataset["dimensions"].length] = {
          name: '总分',
          type: 'line',
          itemStyle: {
            normal: {
              "color": "rgba(252,230,48,1)",
              "label": {
                "show": true,
                "position": "top"
              }
            }
          },
          data: array
        }
        //加载图表数据
        var option = JSON.parse(JSON.stringify(line1Option));
        option.series = series;
        option.dataset = dataset;
        line1.setOption(option);
        //加载雷达图数据
        initRadar(dataset["source"][0], dataset["maxPoint"]);

        line1.on('click', function(params) {
          if(params.seriesType == "line"){
        	  return;
          }
          lineClick(params, "bar1");
        });
        //点击指标项事件zjf
        line1.on('legendselectchanged', function(a) {
          legendClick(line1, a, dataset["source"], dataset["dimensions"], dataset);
        });

        //雷达图指标项点击事件
        $("#leida-list").on("click", "li.leidali", function(param) {
          var titles = $(this).attr("data");
          $.ajax({
            url: contextPath + "/aasess/data/api/getDataByTypeId",
            data: {
              parentCode: parentCodes,
              aasessValueId: aasessValue,
              typeId: $(this).attr("id")
            },
            type: "post",
            success: function(res) {
              //表头数据
              var html = "<thead><tr></tr>";
              for (var index = 0; index < res.dimensions.length; index++) {
                html += "<th class=\"td\">" + res.dimensions[index] + "</th>";
              }
              //表格数据
              html += "</tr></thead><tbody>";
              for (var index = 0; index < res.source.length; index++) {
                html += "<tr>"
                for (var i = 0; i < res.source[index].length; i++) {
                  html += "<td class=\"td\">" + res.source[index][i] + "</td>";
                }
                html += "</tr>"
              }

              $("#zbtable").html(html);
              layer.open({
                type: 1,
                area: ['2786px', '1000px'],
                zIndex: 999,
                skin: 'layer-ext', // 自定义样式
                closeBtn: 0,
                title: titles,
                shadeClose: true,
                shade: [0.5, '#091d3f'],
                content: $("#zbinfo"),
                btn: [''],
                yes: function(index, layero) {
                  layer.closeAll();
                }
              });
            }
          });
        });
        //加载timeline
        var html = "";
        for (var index = 0; index < timelines.length; index++) {
          var elements = timelines[index];
          if (aasessValue === "") {
            aasessValue = elements[0];
          }
          html += '<a class="timeline__item" href="javascript:;" data="' + elements[0] + '" index=' + index + ' >';
          html += '<div class="timeline__content">' + elements[1] + '</div>';
          html += '<div class="timeline__itemafter"></div>';
          html += ' </a>';
        }
        $("#timeline__items").html(html);
        timelineLsngth = timelines.length - 1;
        $('.timeline').timeline({
          forceVerticalMode: 500, //低于这个宽度开始变形
          mode: 'horizontal',
          visibleItems: timelines.length
        });
        $("div.timeline__content").first().addClass("timeline__contents");
        $("div.timeline__itemafter").first().addClass("timeline__itemafters");
        //timeline点击事件
        $("a.timeline__item").on("click", function() {
        	//zjf 解决从其他期选择完非试点的 跳转其他期后值不是试点
            parentCodes = "city.sd";
          //var code = $(this).attr("data");
          aasessValue = $(this).attr("data");
          aindex = $(this).attr("index");
          $("div.timeline__content").removeClass("timeline__contents");
          $("div.timeline__itemafter").removeClass("timeline__itemafters");
          $(this).find("div.timeline__content").addClass("timeline__contents");
          $(this).find("div.timeline__itemafter").addClass("timeline__itemafters");
          var line1 = echarts.getInstanceByDom(document.getElementById("bar1"));
          loadCharData(line1, JSON.parse(JSON.stringify(line1Option)), {
            parentCode: "city.sd",
            aasessValueId: aasessValue,
            typeId: sdtypeId
          }, true);
          var line2 = echarts.getInstanceByDom(document.getElementById("bar2"));
          loadCharData(line2, JSON.parse(JSON.stringify(line2Option)), {
            parentCode: "city.fsd",
            aasessValueId: aasessValue,
            typeId: fsdtypeId
          });
          var line3 = echarts.getInstanceByDom(document.getElementById("bar3"));
          loadCharData(line3, JSON.parse(JSON.stringify(line3Option)), {
            parentCode: "sz.ywb",
            aasessValueId: aasessValue,
            typeId: ywbtypeId
          });
          var line4 = echarts.getInstanceByDom(document.getElementById("bar4"));
          loadCharData(line4, JSON.parse(JSON.stringify(line4Option)), {
            parentCode: "sz.wwb",
            aasessValueId: aasessValue,
            typeId: wwbtypeId
          });
          var line5 = echarts.getInstanceByDom(document.getElementById("bar5"));
          loadCharData(line5, JSON.parse(JSON.stringify(line5Option)), {
            parentCode: "sz.ywbwzz",
            aasessValueId: aasessValue,
            typeId: ywbwzztypeId
          });
        });
      }
    });
  }

  // 定时执行
  // var t_IntervaClick = window.setInterval(function () {
  //   if (parseInt(aindex) == timelineLsngth) {
  //     aindex = -1;
  //   }
  //   $('a.timeline__item[index="' + (parseInt(aindex) + 1) + '"]').click();
  // }, 3000);
  //加载图表数据
  function loadCharData(line1, option, params, radar) {
    $.ajax({
      url: contextPath + "/aasess/data/api/getDataByCode",
      data: params,
      type: "post",
      success: function(res) {
        var array = new Array();
        var dataset = res["dataset"];
        var series = new Array(dataset["dimensions"].length - 1);
        for (var index = 0; index < dataset["dimensions"].length - 1; index++) {
          series[index] = {
            type: 'bar',
            stack: '总量',
            label: {
              normal: {
                show: false,
                position: 'insideRight'
              }
            }
          };
        }
        for (var i = 0; i < dataset["source"].length; i++) {
          array.push(sum(dataset["source"][i]));
        }
        //series[dataset["dimensions"].length] = {"name": "总数","type": "bar","stack": "总量","itemStyle": { "normal": {"label": {"show": true,"position": "insideBottomLeft","textStyle": {"color": "#e4e4e4"}},"color": "#1D222A"}},"data": array}
        series[dataset["dimensions"].length] = {
          name: '总分',
          type: 'line',
          itemStyle: {
            normal: {
              "color": "rgba(252,230,48,1)",
              "label": {
                "show": true,
                "position": "top"
              }
            }
          },
          data: array
        }
        option.series = series;
        option.dataset = dataset;
        //zjf跳转不至于不同期数也是全不选中
        var array = dataset.dimensions;
        var obj = {};
        for (var key in array) {
          obj[array[key]] = true;
        }
        option.legend.selected = obj;
        //zjf
        //先清空数据
        line1.clear();
        line1.setOption(option);
        if (radar) {
          initRadar(dataset["source"][0], dataset["maxPoint"]);
        }
      }
    });
  }

  //图表点击事件
  function lineClick(params, charName) {
	if(params.seriesType == "line"){
		return;
    }
    var line1 = echarts.getInstanceByDom(document.getElementById(charName));
    var leida = echarts.getInstanceByDom(document.getElementById("leida"));
    var option = line1.getOption();
    parentCodes = option.parentCode;
    var maxPoint = option.dataset[0]["maxPoint"];
    var data = params.data;
    var indicator = new Array();
    var series = new Array();
    var name = "";
    var html = "<ul>";
    var sum = 0;
    var re = /^[0-9a-zA-Z]*$/g;
    for (var i in data) {
      if (i == "name") {
        name = data[i];
      } else {
        if (!re.test(i)) {
          html += '<li class="leidali" id="' + getDataIdByName(data, i) + '" data="' + i + '">' + i + ' ' +
            data[i] + '/' + maxPoint[i] + '分</li>';
          indicator.push({
            "name": i,
            "max": maxPoint[i]
          });
          series.push(data[i]);
          sum += parseFloat(data[i]);
        }
      }
    }
    html += '</ul>';
    var zf = '<ul><li style="color:#EE6363;font-size:28px">总分：' + parseFloat(sum).toFixed(4) + ' 分</li>' + html + '</ul>';
    $("#leida-list").html(zf);
    $("#leida-title").html(name);
    var option = {
      //      title: { text: name },
      radar: {
        indicator: indicator
      },
      series: [{
        name: '得分 vs 总分',
        type: 'radar',
        data: [{
          value: series
        }]
      }]
    };
    leida.setOption(option);
  }

  function getDataIdByName(data, name) {
    var id = "";
    for (var i in data) {
      if (data[i] === name) {
        id = i;
      }
    }
    return id;
  }

  //初始化雷达图
  function initRadar(data, maxPoint) {
    var leida = echarts.init(document.getElementById("leida"));
    var indicator = new Array();
    var series = new Array();
    var name = "";
    var html = "<ul>";
    var sum = 0;
    var re = /^[0-9a-zA-Z]*$/g;
    for (var i in data) {
      if (i == "name") {
        name = data[i];
      } else {
        if (!re.test(i)) {
          html += '<li  class="leidali" id="' + getDataIdByName(data, i) + '" data="' + i + '">' + i + ' ' +
            data[i] + '/' + maxPoint[i] + '分</li>';
          indicator.push({
            "name": i,
            "max": maxPoint[i]
          });
          series.push(data[i]);
          sum += parseFloat(data[i]);
        }
      }
    }
    html += '</ul>';
    $("#leida-list").html('<ul><li style="color:#EE6363;font-size:28px">总分：' + parseFloat(sum).toFixed(4) + ' 分</li>' + html + '</ul>');
    $("#leida-title").html(name);
    var laOption = {
      //title: { text: name, textStyle: { color: "#2171c1" } },
      grid: {
        x: 40,
        x2: 100,
        y1: 100,
        y2: 150
      },
      radar: {
        name: {
          textStyle: {
            color: '#ccffcc',
            fontSize: "18"
          }
        },
        indicator: indicator,
        splitArea: {
          show: false,
          areaStyle: {
            color: ["#2a4a93"]
          }
        },
        splitLine: {
          show: true,
          lineStyle: {
            width: 1,
            color: '#3071bf'
          }
        },
        axisLine: {
          show: false,
          lineStyle: {
            color: '#1e6db3',
            width: 1,
            type: 'solid'
          }
        },
      },
      series: [{
        name: '得分 vs 总分',
        type: 'radar',
        areaStyle: {
          normal: {
            color: '#d0397d',
          }
        },
        data: [{
          value: series
        }]
      }],
    };
    leida.setOption(laOption);

  }

  //非试点
  function initFSDdata() {
    var line2 = echarts.init(document.getElementById("bar2"));
    loadCharData(line2, JSON.parse(JSON.stringify(line2Option)), {
      parentCode: "city.fsd",
      aasessValueId: "",
      typeId: fsdtypeId
    });
    line2.on('click', function(params) {
      lineClick(params, "bar2");
    });
  }

  //有网办
  function provincefun() {
    var line3 = echarts.init(document.getElementById("bar3"));
    loadCharData(line3, JSON.parse(JSON.stringify(line3Option)), {
      parentCode: "sz.ywb",
      aasessValueId: "",
      typeId: ywbtypeId
    });
    line3.on('click', function(params) {
      lineClick(params, "bar3");
    });
  }
  //加载省直有网办无证照
  function provinceNolicencefun() {
    var line5 = echarts.init(document.getElementById("bar5"));
    loadCharData(line5, JSON.parse(JSON.stringify(line5Option)), {
      parentCode: "sz.ywbwzz",
      aasessValueId: "",
      typeId: ywbwzztypeId
    });
    line5.on('click', function(params) {
      lineClick(params, "bar5");
    });
  }
  //加载省直无网办综合得分
  function noNetoffices() {
    var line4 = echarts.init(document.getElementById("bar4"));
    loadCharData(line4, JSON.parse(JSON.stringify(line4Option)), {
      parentCode: "sz.wwb",
      aasessValueId: "",
      typeId: wwbtypeId
    });
    line4.on('click', function(params) {
      lineClick(params, "bar4");
    });
  }

  //计算总分公用方法
  function sum(data) {
    var re = /^[0-9a-zA-Z]*$/g;
    var s = 0;
    for (var i in data) {
      if (i == "name") {
        name = data[i];
      } else {
        if (!re.test(i)) {
          s += parseFloat(data[i]);
        }
      }
    }
    return s.toFixed(4);
  }


  //加载试点图表数据
  initAllData();
  //加载非试点图表数据
  initFSDdata();
  provincefun();
  noNetoffices();
  provinceNolicencefun();

})

//柱状图筛选事项全选/不全选按钮
function selectall(index) {
  var selectArr = echarts.init(document.getElementById("bar" + index)).getOption().dataset[0].dimensions;
  var flag = $(this).attr('flag');
  var linename = {};
  if (index == 1) {
    linename = line1Option
  } else if (index == 2) {
    linename = line2Option
  } else if (index == 3) {
    linename = line3Option
  } else if (index == 4) {
    linename = line4Option
  } else if (index == 5) {
    linename = line5Option
  }
  if (linename.legend.selected == null) {
    val = false;
  } else {
    if (flag == 1) {
      var val = false;
      $(this).attr('flag', 0);
    } else {
      var val = true;
      $(this).attr('flag', 1);
    }
  }
  var obj = {};
  for (var key in selectArr) {
    obj[selectArr[key]] = val;
  }

  linename.legend.selected = obj;
  //line3Option.legend.selected = obj;
  echarts.init(document.getElementById("bar" + index)).setOption(linename);
}